package tofile

import (
	"fmt"
	"gitee.com/hexug/go-tools/common/validate"
)

var AppName = "tofile"

type DataCore struct {
	Path     string   `json:"path" validate:"required"`
	YamlData [][]byte `json:"data" validate:"required"`
}

func NewDataCore(path string, data [][]byte) *DataCore {
	return &DataCore{
		Path:     path,
		YamlData: data,
	}
}

type WriterYamlRequest struct {
	Items []*DataCore `json:"items"`
}

func (r *WriterYamlRequest) Validate() error {
	if len(r.Items) == 0 {
		return fmt.Errorf("items is empty")
	}
	for _, item := range r.Items {
		if err := validate.V().Struct(item); err != nil {
			return err
		}
	}
	return nil
}

func NewWriterYamlRequest() *WriterYamlRequest {
	return &WriterYamlRequest{
		Items: make([]*DataCore, 0),
	}
}
func (r *WriterYamlRequest) Add(path string, data [][]byte) *WriterYamlRequest {
	r.Items = append(r.Items, NewDataCore(path, data))
	return r
}
